using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.Remoting.Messaging;
using System.Threading;
using System.Windows.Forms;
using Tool.BaseClass;
using Tool.Model;
using Tool.Utilty;

namespace Tool
{
	public class DataNoEixstsFile : UserControl
	{
		public delegate bool GDelegate2(string url, string path, int Millisecond, string UserAgent);

		private int Total = 0;

		private int DownNumber = 0;

		private int Success = 0;

		private int Fail = 0;

		private int TimeOut = 30000;

		private System.ComponentModel.IContainer components = null;

		private DataGridView gv_list;

		private Button bt_export;

		private Label lb_msg;

		private Button bt_down;

		private System.ComponentModel.BackgroundWorker bgw;

		private DataGridViewLinkColumn NoExistsUrl;

		private DataGridViewTextBoxColumn NoExistsPath;

		private Button btnClear;

		public DataNoEixstsFile()
		{
			this.InitializeComponent();
		}

		private void DataNoEixstsFile_Load(object sender, EventArgs e)
		{
			this.gv_list.AutoGenerateColumns = false;
			this.gv_list.BackgroundColor = System.Drawing.Color.White;
			this.gv_list.RowHeadersVisible = false;
			this.gv_list.AllowUserToAddRows = false;
			this.gv_list.AllowUserToDeleteRows = false;
			this.gv_list.MultiSelect = false;
			this.gv_list.ReadOnly = true;
			this.gv_list.SelectionMode = DataGridViewSelectionMode.CellSelect;
			this.gv_list.DataSource = new System.ComponentModel.BindingList<GClass31>(this.method_0());
			this.lb_msg.Text = "共“" + this.gv_list.Rows.Count.ToString() + "”项";
			this.lb_msg.ForeColor = System.Drawing.Color.Green;
		}

		private List<GClass31> method_0()
		{
			return GClass28.smethod_4();
		}

		private void bt_export_Click(object sender, EventArgs e)
		{
			List<GClass31> list = this.method_0();
			if (list.Count <= 0)
			{
				this.lb_msg.Text = "数据为空,导出失败.";
				this.lb_msg.ForeColor = System.Drawing.Color.Red;
			}
			else
			{
				SaveFileDialog saveFileDialog = new SaveFileDialog();
				saveFileDialog.Filter = "Html Document(*.html)|*.html";
				saveFileDialog.FileName = "BrokenLinkRecords.html";
				if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
				{
					StreamWriter streamWriter = new StreamWriter(saveFileDialog.FileName);
					string text = "<table class=\"tablelistnowrap\" cellspacing=\"0\" cellpadding=\"0\">";
					text += "<tr>";
					text += "<th width=\"50%\">网址</th>";
					text += "<th width=\"50%\">保存路径</th>";
					text += "</tr>";
					foreach (GClass31 current in list)
					{
						text += "<tr>";
						string text2 = text;
						text = string.Concat(new string[]
						{
							text2,
							"<td><a href=\"",
							current.NoExistsUrl,
							"\" target=\"_blank\">",
							current.NoExistsUrl,
							"</a></td>"
						});
						text = text + "<td>" + current.NoExistsPath + "</td>";
						text += "</tr>";
					}
					text += "</table>";
					streamWriter.Write(GClass0.smethod_11("链接失效记录", text));
					streamWriter.Close();
				}
			}
		}

		private void bt_down_Click(object sender, EventArgs e)
		{
			List<GClass31> list = this.method_0();
			if (list.Count <= 0)
			{
				this.lb_msg.Text = "数据为空,拒绝执行.";
				this.lb_msg.ForeColor = System.Drawing.Color.Red;
			}
			else if (!this.bgw.IsBusy)
			{
				this.Total = list.Count;
				this.Fail = 0;
				this.Success = 0;
				this.DownNumber = 0;
				List<GClass50> list2 = new List<GClass50>();
				foreach (GClass31 current in list)
				{
					list2.Add(new GClass50
					{
						Id = current.NoExistsId,
						Url = current.NoExistsUrl,
						Path = current.NoExistsPath
					});
				}
				try
				{
					this.bgw.RunWorkerAsync(list2);
				}
				catch (Exception ex)
				{
					this.lb_msg.Text = ex.Message.ToString();
				}
				this.bt_down.Enabled = true;
			}
			else
			{
				this.lb_msg.Text = "正在运行中";
				this.lb_msg.ForeColor = System.Drawing.Color.Red;
			}
		}

		private void method_1(List<GClass50> list_0)
		{
			IAsyncResult asyncResult = null;
			using (List<GClass50>.Enumerator enumerator = list_0.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					GClass50 current = enumerator.Current;
					GClass50 gClass = new GClass50();
					gClass.Id = current.Id;
					gClass.Url = current.Url;
					gClass.Path = current.Path;
					gClass.TotalNumber = list_0.Count;
					GClass40 @object = new GClass40();
					DataNoEixstsFile.GDelegate2 gDelegate = new DataNoEixstsFile.GDelegate2(@object.method_0);
					asyncResult = gDelegate.BeginInvoke(current.Url, current.Path, this.TimeOut, GClass0.UserAgent, new AsyncCallback(this.method_2), gClass);
				}
				goto IL_B2;
			}
			IL_AB:
			Thread.Sleep(50);
			IL_B2:
			if (asyncResult.IsCompleted)
			{
				return;
			}
			goto IL_AB;
		}

		private void method_2(IAsyncResult iasyncResult_0)
		{
			AsyncResult asyncResult = (AsyncResult)iasyncResult_0;
			GClass50 gClass = (GClass50)asyncResult.AsyncState;
			DataNoEixstsFile.GDelegate2 gDelegate = (DataNoEixstsFile.GDelegate2)asyncResult.AsyncDelegate;
			gClass.IsDown = gDelegate.EndInvoke(iasyncResult_0);
			this.DownNumber++;
			int int_ = this.method_4(this.DownNumber, gClass.TotalNumber);
			if (!gClass.IsDown)
			{
				this.Fail++;
			}
			else
			{
				this.Success++;
			}
			gClass.Sucess = this.Success;
			gClass.Fail = this.Fail;
			gClass.Action = GClass50.GEnum7.Message;
			this.method_3(this.bgw, int_, gClass);
		}

		private void method_3(System.ComponentModel.BackgroundWorker backgroundWorker_0, int int_0, object object_0)
		{
			try
			{
				backgroundWorker_0.ReportProgress(int_0, object_0);
			}
			catch
			{
			}
		}

		private int method_4(int int_0, int int_1)
		{
			int result = 0;
			if (int_0 <= 0 || int_0 >= int_1)
			{
				if (int_0 == int_1)
				{
					result = 100;
				}
			}
			else
			{
				result = (int)(Convert.ToDecimal(int_0) / Convert.ToDecimal(int_1) * 100m);
			}
			return result;
		}

		private void bgw_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
		{
			this.method_3(this.bgw, 0, new GClass50
			{
				Action = GClass50.GEnum7.Begin
			});
			List<GClass50> list_ = (List<GClass50>)e.Argument;
			this.method_1(list_);
		}

		private void bgw_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
		{
			GClass50 gClass = (GClass50)e.UserState;
			switch (gClass.Action)
			{
			case GClass50.GEnum7.Begin:
				this.bt_down.Enabled = false;
				this.lb_msg.Text = "准备下载……";
				this.lb_msg.ForeColor = System.Drawing.Color.Blue;
				break;
			case GClass50.GEnum7.Message:
				this.lb_msg.Text = string.Concat(new string[]
				{
					"已下载:",
					e.ProgressPercentage.ToString(),
					"%，成功:",
					gClass.Sucess.ToString(),
					"，失败:",
					gClass.Fail.ToString()
				});
				this.lb_msg.ForeColor = System.Drawing.Color.Blue;
				if (gClass.IsDown)
				{
					List<GClass31> list = GClass28.smethod_4();
					foreach (GClass31 current in list)
					{
						if (current.NoExistsId.Equals(gClass.Id))
						{
							list.Remove(current);
							break;
						}
					}
				}
				if (this.DownNumber == gClass.TotalNumber)
				{
					this.gv_list.DataSource = new System.ComponentModel.BindingList<GClass31>(this.method_0());
				}
				break;
			}
		}

		private void bgw_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
		{
			this.bt_down.Enabled = true;
			this.lb_msg.Text = string.Concat(new string[]
			{
				"已完成:",
				this.method_4(this.Success + this.Fail, this.Total).ToString(),
				"%，成功:",
				this.Success.ToString(),
				"，失败:",
				this.Fail.ToString()
			});
			this.gv_list.DataSource = new System.ComponentModel.BindingList<GClass31>(this.method_0());
		}

		private void gv_list_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.ColumnIndex == 0)
			{
				System.Diagnostics.Process.Start("explorer.exe", this.gv_list.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
			}
		}

		private void btnClear_Click(object sender, EventArgs e)
		{
			List<GClass31> list = GClass28.smethod_4();
			list.Clear();
			this.DataNoEixstsFile_Load(sender, e);
		}

		//protected override void vmethod_0(bool bool_0)
		//{
		//	if (bool_0 && this.components != null)
		//	{
		//		this.components.Dispose();
		//	}
		//	base.Dispose(bool_0);
		//}

		private void InitializeComponent()
		{
			this.gv_list = new DataGridView();
			this.NoExistsUrl = new DataGridViewLinkColumn();
			this.NoExistsPath = new DataGridViewTextBoxColumn();
			this.bt_export = new Button();
			this.lb_msg = new Label();
			this.bt_down = new Button();
			this.bgw = new System.ComponentModel.BackgroundWorker();
			this.btnClear = new Button();
			((System.ComponentModel.ISupportInitialize)this.gv_list).BeginInit();
			base.SuspendLayout();
			this.gv_list.BackgroundColor = System.Drawing.Color.White;
			this.gv_list.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.gv_list.Columns.AddRange(new DataGridViewColumn[]
			{
				this.NoExistsUrl,
				this.NoExistsPath
			});
			this.gv_list.Location = new System.Drawing.Point(12, 12);
			this.gv_list.Name = "gv_list";
			this.gv_list.RowHeadersVisible = false;
			this.gv_list.RowTemplate.Height = 23;
			this.gv_list.Size = new System.Drawing.Size(460, 250);
			this.gv_list.TabIndex = 0;
			this.gv_list.CellContentClick += new DataGridViewCellEventHandler(this.gv_list_CellContentClick);
			this.NoExistsUrl.DataPropertyName = "NoExistsUrl";
			this.NoExistsUrl.HeaderText = "网址";
			this.NoExistsUrl.Name = "NoExistsUrl";
			this.NoExistsUrl.ReadOnly = true;
			this.NoExistsUrl.Resizable = DataGridViewTriState.True;
			this.NoExistsUrl.SortMode = DataGridViewColumnSortMode.Automatic;
			this.NoExistsUrl.Width = 215;
			this.NoExistsPath.DataPropertyName = "NoExistsPath";
			this.NoExistsPath.HeaderText = "保存路径";
			this.NoExistsPath.Name = "NoExistsPath";
			this.NoExistsPath.ReadOnly = true;
			this.NoExistsPath.Width = 215;
			this.bt_export.Location = new System.Drawing.Point(376, 277);
			this.bt_export.Name = "bt_export";
			this.bt_export.Size = new System.Drawing.Size(75, 23);
			this.bt_export.TabIndex = 1;
			this.bt_export.Text = "导出记录";
			this.bt_export.UseVisualStyleBackColor = true;
			this.bt_export.Click += new EventHandler(this.bt_export_Click);
			this.lb_msg.AutoSize = true;
			this.lb_msg.Location = new System.Drawing.Point(94, 280);
			this.lb_msg.Name = "lb_msg";
			this.lb_msg.Size = new System.Drawing.Size(69, 19);
			this.lb_msg.TabIndex = 2;
			this.lb_msg.Text = "共“0”项";
			this.bt_down.Location = new System.Drawing.Point(13, 276);
			this.bt_down.Name = "bt_down";
			this.bt_down.Size = new System.Drawing.Size(75, 23);
			this.bt_down.TabIndex = 3;
			this.bt_down.Text = "重新下载";
			this.bt_down.UseVisualStyleBackColor = true;
			this.bt_down.Click += new EventHandler(this.bt_down_Click);
			this.bgw.WorkerReportsProgress = true;
			this.bgw.DoWork += new System.ComponentModel.DoWorkEventHandler(this.bgw_DoWork);
			this.bgw.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.bgw_ProgressChanged);
			this.bgw.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.bgw_RunWorkerCompleted);
			this.btnClear.Location = new System.Drawing.Point(295, 276);
			this.btnClear.Name = "btnClear";
			this.btnClear.Size = new System.Drawing.Size(75, 23);
			this.btnClear.TabIndex = 4;
			this.btnClear.Text = "清空记录";
			this.btnClear.UseVisualStyleBackColor = true;
			this.btnClear.Click += new EventHandler(this.btnClear_Click);
			base.AutoScaleDimensions = new System.Drawing.SizeF(7f, 17f);
			base.AutoScaleMode = AutoScaleMode.Font;
			base.Controls.Add(this.btnClear);
			base.Controls.Add(this.bt_down);
			base.Controls.Add(this.lb_msg);
			base.Controls.Add(this.bt_export);
			base.Controls.Add(this.gv_list);
			this.Font = new System.Drawing.Font("微软雅黑", 12f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, 134);
			base.Margin = new Padding(3, 4, 3, 4);
			base.Name = "DataNoEixstsFile";
			base.Size = new System.Drawing.Size(484, 312);
			base.Load += new EventHandler(this.DataNoEixstsFile_Load);
			((System.ComponentModel.ISupportInitialize)this.gv_list).EndInit();
			base.ResumeLayout(false);
			base.PerformLayout();
		}
	}
}
